package com.qinggeng.framework.config.mq.impl;

import com.alibaba.fastjson.JSON;
import com.qinggeng.common.enums.AppTurnPageEnum;
import com.qinggeng.common.enums.MessageTypeEnum;
import com.qinggeng.common.enums.UserTypeEnum;
import com.qinggeng.common.exception.CustomException;
import com.qinggeng.common.utils.DateUtils;
import com.qinggeng.common.utils.StringUtils;
import com.qinggeng.framework.config.mq.dto.MessageOrdersDTO;
import com.qinggeng.framework.config.mq.dto.MessageRedEnvelopesDTO;
import com.qinggeng.framework.config.mq.handle.MessageHandle;
import com.qinggeng.project.system.domain.*;
import com.qinggeng.project.system.service.IOrdersService;
import com.qinggeng.project.system.service.IUserAliasService;
import com.qinggeng.project.system.service.IUserMessageService;
import com.qinggeng.project.system.service.MessagePushService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.undo.CannotUndoException;

import static com.qinggeng.common.enums.OrdersStatusCodeEnum.getOrdersStatusCode;

@Slf4j
@Component
public class RedEnvelopesMessageJPushHandleImpl implements MessageHandle {

    @Autowired
    private IUserAliasService userAliasService;
    @Autowired
    private MessagePushService messagePushService;
    @Autowired
    private IUserMessageService userMessageService;
    @Autowired
    private IOrdersService ordersService;

    @Override
    public void handle(String msg) {

        log.info("红包消息推送处理{}"+msg);

        MessageOrdersDTO ordersDTO=JSON.parseObject(msg,MessageOrdersDTO.class);
        Orders orders=ordersService.selectOrdersByOrdersNo(ordersDTO.getOrdersNo());
        if(null==orders){
            throw new CustomException("订单不存在");
        }
        //根据用户ID查询普通用户的别名
        UserAlias userAlias=userAliasService.selectMemberUserAliasByUserId(orders.getUserId());
        if(null==userAlias|| StringUtils.isEmpty(userAlias.getAlias())){
            return;
        }
        String message="您的返现金额已存到零钱中，快去看看吧！";
        try{

            //消息推送
            JPushExtraModel extra=new JPushExtraModel(AppTurnPageEnum.RED_ENVELOPES,orders.getUserId().toString());
            messagePushService.JPushMessage(userAlias,message,extra);

            UserMessage userMessage=new UserMessage();
            userMessage.setUserId(orders.getUserId());
            userMessage.setMessageType(MessageTypeEnum.零钱消息.getValue());
            userMessage.setFkId(orders.getUserId());
            userMessage.setFkMark("零钱到账通知");
            userMessage.setFkContent(message);
            //userMessage.setStatusCode(getOrdersStatusCode(orders).getCode());
            userMessage.setCreateTime(DateUtils.getNowDate());
            int rows=userMessageService.insertUserMessage(userMessage);
            if(rows<1){
                log.error("保存消息记录失败");
            }

        }catch (Exception e){
            log.error(e.getMessage());
        }
    }
}
